package com.emm.yixun.mobile.ui;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.emm.yixun.mobile.R;
import com.emm.yixun.mobile.adapter.NextUpDateAadapter;
import com.emm.yixun.mobile.application.EmmApplication;
import com.emm.yixun.mobile.base.BaseActivity;
import com.emm.yixun.mobile.model.GetNextReminderList;
import com.emm.yixun.mobile.model.GetNextReminderList.NextReminderList;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import cn.swu.pulltorefresh.PullToRefreshSwipeMenuListView;
import cn.swu.pulltorefresh.PullToRefreshSwipeMenuListView.IXListViewListener;
import me.imid.swipebacklayout.lib.SwipeBackLayout;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;
import xf.tools.Loading;

/**
 * <p>
 * <b>Title：</b> 下次未跟进提醒
 * </p>
 * <p>
 * <b>Description：</b>
 * </p>
 * 
 * @author tangxin
 * @date 2016年12月6日 下午3:38:14
 */
public class NextUpDateActivity extends SwipeBackActivity implements IXListViewListener {

	private static final String TAG = "NextUpDateActivity";

	/** 标题 */
	TextView title_main;

	/** 返回按钮 */
	ImageView back_btn;

	/** 列表 */
	PullToRefreshSwipeMenuListView listView_notice;

	/** 适配器 */
	NextUpDateAadapter adapter;

	/** 侧滑关闭框架 */
	private SwipeBackLayout mSwipeBackLayout;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.nextupdateactivity);

		SetSwipeBackLayout(mSwipeBackLayout);

		InitView();

		SetRelativiLayoutPadding(R.id.main_head_top_rela);
		
		InitBtn();
		getNextReminderList("" + 1, "" + 10, 1);
	}

	/**
	 * 
	 * <p>
	 * <b>Title：</b> 初始化view
	 * </p>
	 * <p>
	 * <b>Description：</b>
	 * </p>
	 * 
	 * @author tangxin
	 * @date 2016年12月7日 下午4:08:58
	 */
	private void InitView() {
		title_main = (TextView) findViewById(R.id.title_main);
		title_main.setText(getString(R.string.nextupdatetext));
		back_btn = (ImageView) findViewById(R.id.back_btn);
		listView_notice = (PullToRefreshSwipeMenuListView) findViewById(R.id.listView_notice2);

		listView_notice.setPullRefreshEnable(true);
		listView_notice.setPullLoadEnable(true);
		listView_notice.setXListViewListener(this);
		listView_notice.setRefreshTime(EmmApplication.getData("RefreshTime_notice_remind_next"));
	}

	/**
	 * 
	 * <p>
	 * <b>Title：</b> 初始化监听事件
	 * </p>
	 * <p>
	 * <b>Description：</b>
	 * </p>
	 * 
	 * @author tangxin
	 * @date 2016年12月7日 下午4:09:08
	 */
	private void InitBtn() {
		back_btn.setVisibility(View.VISIBLE);
		back_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	@Override
	protected void onResume() {
		BaseActivity.activity = this;
		super.onResume();
	}

	/** 刷新 */
	@Override
	public void onRefresh() {
		listView_notice.stopLoadMore();
		h.sendEmptyMessage(1);
	}

	/** 加载更多 */
	@Override
	public void onLoadMore() {
		listView_notice.stopRefresh();
		h.sendEmptyMessage(2);
	}

	/** 模拟加载效果 */
	Handler h = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (msg.what == 1) {// 刷新
				pageNumber = 1;
				getNextReminderList("" + 1, "" + pageSize, 1);
			} else if (msg.what == 2) {// 加载
				pageNumber++;
				getNextReminderList("" + pageNumber, "" + pageSize, 2);

			}
		};
	};

	/** 页码 */
	int pageNumber = 1;

	/** 页码条数 */
	int pageSize = 10;

	boolean IsLoading = true;

	GetNextReminderList getnextreminder;

	/**
	 * 
	 * <p>
	 * <b>Title：</b> 个人信息查询接口
	 * </p>
	 * <p>
	 * <b>Description：</b>
	 * </p>
	 * 
	 * @param pageNumberd
	 * @param pageSized
	 * @param opentype
	 * @author tangxin
	 * @date 2016年12月7日 下午4:09:23
	 */
	private void getNextReminderList(String pageNumberd, String pageSized, final int opentype) {
		EmmApplication.updateUrl("getNextReminderList");
		Map<String, String> map = new HashMap<String, String>();
		map.put("pageSize", pageSized);
		map.put("pageNumber", pageNumberd);
		map.put("version", EmmApplication.getPackageManager(5));
		map.put("merchantId", EmmApplication.getData(EmmApplication.merchantId));
		map.put("token", EmmApplication.getToken());
		map.put("userId", EmmApplication.getUserId());
		map.put("projectCode", EmmApplication.getProjectCode());
		map.put("terminalCode", EmmApplication.getPackageManager(2));
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("params", map);
		JSONObject json = new JSONObject(paramsMap);
		String jsonstr = json.toString();
		RequestParams params = new RequestParams();
		params.put("body", jsonstr);
		Log.v("getMemberInfo-->post", EmmApplication.urlStr + "?body=" + jsonstr);
		AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
		asyncHttpClient.post(EmmApplication.urlStr, params, new AsyncHttpResponseHandler() {
			@Override
			public void onFailure(Throwable error, String content) {
				super.onFailure(error, content);
				Loading.hideDialogForLoading();
				Log.v(TAG, "请求结束error:" + error + "==" + content);
				listView_notice.stopRefresh();
				String timed = EmmApplication.dataTime(EmmApplication.geTime(), "yyyy-MM-dd HH:mm");
				listView_notice.setRefreshTime(timed);
				listView_notice.stopLoadMore();
				EmmApplication.setData("RefreshTime_notice_remind_next", timed);
			}

			@Override
			public void onFinish() {
				Loading.hideDialogForLoading();
				Log.v(TAG, "请求结束");
				listView_notice.stopRefresh();
				String timed = EmmApplication.dataTime(EmmApplication.geTime(), "yyyy-MM-dd HH:mm");
				listView_notice.setRefreshTime(timed);
				listView_notice.stopLoadMore();
				EmmApplication.setData("RefreshTime_notice_remind_next", timed);
				super.onFinish();
			}

			@Override
			public void onStart() {
				super.onStart();
				Loading.hideDialogForLoading();
				Loading.showDialogForLoading(NextUpDateActivity.this, "正在加载...", false, true, 60000);
				Log.v(TAG, "请求开始");
			}

			@Override
			public void onSuccess(String content) {
				super.onSuccess(content);
				if (content == null) {
					return;
				}
				Log.v("content", "content=>" + content);
				JSONObject jsonObj = JSONObject.parseObject(content);
				JSONObject resData = (JSONObject) jsonObj.get("resData");
				if (resData == null) {
					Log.v(TAG, "信息返回值为空");
					return;
				}

				GetNextReminderList getnext = JSONObject.parseObject(resData.toString(), GetNextReminderList.class);

				if ("1".equals(getnext.getResult())) {
					Log.v("获取成功", "");

					if (getnext.getNextReminderList() != null) {
						if (opentype == 1) {// 刷新
							getnextreminder = new GetNextReminderList();
							getnextreminder = getnext;
						} else {// 加载
							if (getnextreminder != null) {
								getnextreminder.getNextReminderList().addAll(getnext.getNextReminderList());
							} else {
								getnextreminder = new GetNextReminderList();
								getnextreminder.getNextReminderList().addAll(getnext.getNextReminderList());
							}
						}
						if (getnextreminder.getNextReminderList().size() < (pageNumber * pageSize)) {// 代表没有更多
							IsLoading = false;
							if (opentype != 1) {
								// EmmApplication.T("没有更多客户");
							}
						}
						if (adapter == null) {
							adapter = new NextUpDateAadapter(getnextreminder.getNextReminderList(),
									NextUpDateActivity.this);
							listView_notice.setAdapter(adapter);
						} else {
							adapter.GetDate(getnextreminder.getNextReminderList());
						}
					} else {
						if (pageNumber > 0 && opentype != 1)
							pageNumber--;
						if (opentype == 1) {
							// EmmApplication.T("没有查询到客户");
						} else {
							// EmmApplication.T("没有更多客户");
						}
						ArrayList<NextReminderList> arrlist;
						if (null != getnextreminder && null != getnextreminder.getNextReminderList()) {
							arrlist = getnextreminder.getNextReminderList();
						} else {
							arrlist = new ArrayList<GetNextReminderList.NextReminderList>();
						}

						if (adapter == null) {
							adapter = new NextUpDateAadapter(arrlist, NextUpDateActivity.this);
							listView_notice.setAdapter(adapter);
						} else {
							adapter.GetDate(arrlist);
						}
					}
				} else {
					EmmApplication.T(getnext.getErrorMsg().toString());
					Log.v("获取失败", "errorCode:" + getnext.getErrorCode().toString() + "errorMsg:"
							+ getnext.getErrorMsg().toString());
				}
			}
		});
	}
}
